/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * exitFrame.java
 *
 * Created on Oct 31, 2011, 7:39:06 PM
 */

package giormi;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ling
 */
public class insufficientstocksFrame extends javax.swing.JFrame {

    /** Creates new form exitFrame */
    private MainFrame main;
    private JFrame frame;
    private F_addorder io, io2;
    private String record;
    private String istocknum;
    private String iqty;
    private F_addorderstocks oss;
    private F_addorderstocks oss2 = new F_addorderstocks();
    protected JTable table;

    public void setOss(F_addorderstocks oss) {
        this.oss = oss;
    }
   
    public insufficientstocksFrame(MainFrame mf, addOrder prev, F_addorder o, JTable table, String stocknum, String qty) {
        initComponents();
        this.setLocation(440, 240);
        this.setSize(450,180);
        this.setResizable(false);
        this.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        this.main=mf;
        this.frame=prev;

        this.iqty=qty;
        this.io=o;
        this.istocknum=stocknum;
        stocknumLabel.setText(istocknum);
        qtyLabel.setText(iqty);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addsuccess1 = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        addsuccess2 = new javax.swing.JLabel();
        addsuccess3 = new javax.swing.JLabel();
        lackofstocks2 = new javax.swing.JLabel();
        stocknumLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        qtyLabel = new javax.swing.JLabel();

        addsuccess1.setText("Confirm Change. Are you sure you wish to");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Confirm Changes");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Close-2-icon.png"))); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 110, 90, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Warning-icon (1).png"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, -1));

        okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Ok-icon.png"))); // NOI18N
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        getContentPane().add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 110, 90, -1));

        addsuccess2.setText("Confirm Change. The stock number selected");
        getContentPane().add(addsuccess2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, 50));

        addsuccess3.setText("cannot handle the order's total quantity. The");
        getContentPane().add(addsuccess3, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 40, -1, 40));

        lackofstocks2.setText("system automatically subtracted");
        getContentPane().add(lackofstocks2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 70, 160, 10));

        stocknumLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        stocknumLabel.setText("<stock num>");
        getContentPane().add(stocknumLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 80, 20));

        jLabel2.setText("from Stock No.");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 80, 80, 20));

        qtyLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        qtyLabel.setText("<qty>");
        getContentPane().add(qtyLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 70, 50, 10));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void setTable(JTable table) {
        this.table = table;
    }
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        frame.setEnabled(true);
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    public void addRow (JTable table, String record){
        if (record.equalsIgnoreCase("stock") || record.equalsIgnoreCase("order"))
        table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.insertRow(model.getRowCount(), new Object[]{""});

        if (record.equalsIgnoreCase("stock") || record.equalsIgnoreCase("order"))
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
}
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        viewOrders v_o = new viewOrders(table);
        io.connect();
        io.addorder();
        io.updatestock();
        io.disconnect();

        io2 = io;
        io2.setO_idstock(Integer.parseInt(istocknum));
        io2.setO_qtysold(iqty);
        io2.setO_qtyreturned("0");
        
//        idos+=1;
        
        oss2.setOs_idstock(Integer.parseInt(istocknum));
        oss2.setOs_idorder(io2.getidorder());
        oss2.addorder_stock();

        io2.setO_idorder_stock(oss2.getidorder_stock());

        System.out.println("qty:"+iqty+" stock:"+istocknum+" order:"+io2.getidorder()+" os:"+oss2.getidorder_stock());

        io2.connect();
        io2.addorder();
        io2.updatestock();
        io2.disconnect();
                    //clear
        addRow(this.table,"order");
        v_o.connect();
        v_o.viewOrders();
        v_o.disconnect();

        this.setVisible(false);
        this.dispose();
        successFrame su = new successFrame(frame, main);
        su.setVisible(true);
    }//GEN-LAST:event_okButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addsuccess1;
    private javax.swing.JLabel addsuccess2;
    private javax.swing.JLabel addsuccess3;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lackofstocks2;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel qtyLabel;
    private javax.swing.JLabel stocknumLabel;
    // End of variables declaration//GEN-END:variables

}
